package cyx.example.ddd.application.order.assembler;

import cyx.example.ddd.application.order.dto.OrderDTO;
import cyx.example.ddd.domain.order.model.aggregate.Order;
import cyx.example.ddd.domain.order.model.entity.OrderItem;
import cyx.example.ddd.domain.order.model.valueobject.Money;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

/**
 * 订单装配器
 * 使用MapStruct进行DTO和领域对象的转换
 */
@Mapper(componentModel = "spring")
public interface OrderAssembler {

    @Mapping(source = "totalAmount.amount", target = "totalAmount")
    @Mapping(source = "totalAmount.currency", target = "currency")
    OrderDTO toDTO(Order order);

    @Mapping(source = "totalPrice.amount", target = "totalPrice")
    @Mapping(source = "unitPrice.amount", target = "unitPrice")
    OrderDTO.OrderItemDTO toDTO(OrderItem orderItem);

    default Double map(Money money) {
        return money != null ? money.getAmount().doubleValue() : null;
    }
}